package com.central.apps.controller;

import com.central.app.common.constant.AppConstant;
import com.central.app.service.HandlerService;
import com.central.common.web.PageResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/check")
public class CheckController {

    @Autowired
    private HandlerService handlerService;

    @PostMapping("/queryInvbalances")
    public PageResult<Map<String, Object>> queryTable(@RequestBody Map<String, Object> params) {

        Integer page = MapUtils.getInteger(params, AppConstant.BIZ_QUERY_PAGE);
        Integer limit = MapUtils.getInteger(params, AppConstant.BIZ_QUERY_LIMIT);
        String  storenum = MapUtils.getString(params, "storenum");
        String  itemnum = MapUtils.getString(params, "itemnum");
        String  itemdesc = MapUtils.getString(params, "itemdesc");
        String  binnum = MapUtils.getString(params, "binnum");
        String materialtype = MapUtils.getString(params, "materialtype");//物料编码
        String  maxnum = MapUtils.getString(params, "maxnum");
        String  mediumnum = MapUtils.getString(params, "mediumnum");
        String  smallnum = MapUtils.getString(params, "smallnum");
        String  subjectno = MapUtils.getString(params, "subjectno");
        String  applyby = MapUtils.getString(params, "applyby");
        String  whereCondition = MapUtils.getString(params, "where_condition");

        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        String sql = "select t.*,g.maxnum,g.mediumnum,g.smallnum,g.materialtype,p.name from gq_invbalances t left join gq_item g on t.itemnum=g.itemnum left join gq_person p on t.applyby=p.id where 1=1 and g.status ='已启用' and t.curbal>0";
        if(StringUtils.isNotEmpty(storenum)) {
            sql = sql + " and t.storenum like '%" + storenum + "%'";
        }else{
            sql = sql + " and t.storenum = null ";
        }
        if(StringUtils.isNotEmpty(itemnum)) {
            sql = sql + " and t.itemnum like '%" + itemnum + "%'";
        }

        if(StringUtils.isNotEmpty(itemdesc)) {
            sql = sql + " and t.itemdesc like '%" + itemdesc + "%'";
        }
        if(StringUtils.isNotEmpty(binnum)) {
            sql = sql + " and t.binnum like '%" + binnum + "%'";
        }

        boolean isnum = false;
        String sql2="";
        if(StringUtils.isNotEmpty(maxnum)) {
            sql2 = sql2 + " and maxnum like '%" + maxnum + "%'";
            isnum = true;
        }
        if(StringUtils.isNotEmpty(mediumnum)) {
            sql2 = sql2 + " and mediumnum like '%" + mediumnum + "%'";
            isnum = true;
        }
        if(StringUtils.isNotEmpty(smallnum)) {
            sql2 = sql2 + " and smallnum like '%" + smallnum + "%'";
            isnum = true;
        }
        if(StringUtils.isNotEmpty(materialtype)) {
            sql2 = sql2 + " and materialtype like '%" + materialtype + "%'";
            isnum = true;
        }
        boolean istrue = false;
        String sql3="";
        if(StringUtils.isNotBlank(subjectno)){
            sql3 = sql3 + " and t.subjectno like '%" + subjectno + "%'";
            istrue = true;
        }
        if(StringUtils.isNotBlank(applyby)){
            sql3 = sql3 + " and t.applyby in ( select id from gq_person where name = '" + applyby + "' )";
            istrue = true;
        }


        if (isnum){

            sql = sql +" and t.itemnum in (select itemnum from gq_item where 1=1 "+sql2+")";
        }
        if(StringUtils.isNotEmpty(whereCondition)) {
            sql = sql + " and  " + whereCondition;
        }

        if(istrue){
            sql = sql + sql3;
        }

        return handlerService.pageList(page, limit, sql);
    }
}
